/**
 * 二分查找
 * @param arr
 * @param findNum
 */
function search(arr, findNum) {
  let lowIndex = 0;
  let highIndex = arr.length - 1;
  let count = 0;
  while (lowIndex <= highIndex) {
    //  改变 lowIndex 和 highIndex
    let midIndex = Math.floor((lowIndex + highIndex) / 2);
    //  arr[midIndex] 和 findNum 大小关系
    count++;
    if (findNum < arr[midIndex]) {
      highIndex = midIndex - 1;
    } else if (findNum > arr[midIndex]) {
      lowIndex = midIndex + 1;
    } else {
      return [midIndex, count];
    }
  }
  return [-1, count];
}

let arr = [];
for (let i = 1; i < 101; i++) {
  arr.push(i);
}
let num = Number(process.argv[2]);
if (!num && num !== 0) {
  throw new TypeError('input value is not a number');
}

let [finIndex, count] = search([...arr], num);
if (finIndex === -1) {
  console.log(`可惜了目标值:${num} 没找到,查找次数:`, count);
} else {
  console.log(`目标值:${num},的下标是:${finIndex}
,查找的次数是${count}`);
}
